package com.example.myapplication.ui.notifications;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.R;

import org.json.JSONException;
import org.json.JSONObject;

public class NotificationsFragment extends Fragment {
    private static final String TAG = "NotificationsFragment";
    Button btnRegister;
    Button btnLogin;
    private NotificationsViewModel notificationsViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                ViewModelProviders.of(this).get(NotificationsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        btnRegister = root.findViewById(R.id.btn_register);
        btnLogin = root.findViewById(R.id.button);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        notificationsViewModel.getLoginBeanMutableLiveData()
                .observe(getViewLifecycleOwner(), new Observer<LoginBean>() {
                    @Override
                    public void onChanged(LoginBean loginBean) {
                        if(loginBean.getCode() == 200 && loginBean.getToken()!=null){
                            Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(getContext(), "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);
                navController.navigate(R.id.registFragment);
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 用户输入数据的获取
                EditText etName = (EditText) (view.findViewById(R.id.editTextTextPersonName));
                EditText etPassword = view.findViewById(R.id.editTextTextPassword);
                String name = etName.getText().toString();
                String password = etPassword.getText().toString();
                notificationsViewModel.getData(getContext(),name, password);
            }
        });
    }
}